/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * relatorioAnimaisporRua.java
 *
 * Created on 29/09/2009, 21:34:04
 */

package cparc;

import com.mysql.jdbc.Statement;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;

/**
 *
 * @author Administrador
 */
public class relatorioAnimaisporRua extends javax.swing.JInternalFrame {

    /** Creates new form relatorioAnimaisporRua */
    public relatorioAnimaisporRua() {
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        btGerar = new javax.swing.JButton();
        tfRua = new javax.swing.JTextField();

        setClosable(true);
        setTitle("Teste de Relatório");

        jLabel2.setText("Rua");

        btGerar.setText("Gerar!");
        btGerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btGerarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(13, 13, 13)
                        .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(btGerar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btGerar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btGerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btGerarActionPerformed

        Statement stm = null;
        String result = null;
        String consulta = "SELECT PCOD FROM CPARC.CAD_PROP WHERE PEND = " + tfRua.getText() + " LIMIT 0,30";
        String prop[] = new String[30];

        try  {
        stm = (Statement) conexao.getConnection(true).createStatement();
        ResultSet rs = stm.executeQuery(consulta);
        int i=0;

        while(rs.next()){

            prop[i] = rs.getString("PCOD");
            //System.out.println(prop[i]);
            i++;
        }
        }
   // em caso de erro apreenta mensagem abaixo
            catch(Exception e){
                e.printStackTrace();

            }



        File file = null;
        try {
     file = new File("Relatorio.txt");

    // Create file if it does not exist
    boolean success = file.createNewFile();
    if (success) {
        
        
    } else {
        // File already exists
    }
} catch (IOException e) {
}
        FileWriter x = null;
        try{
      // o true significa q o arquivo será constante
      x = new FileWriter(file,true);



      x.write("--------------------RELATRORIO ANIMAIS POR RUA--------------------\r\n\r\n");
      x.write("--------------------RUA "+tfRua.getText()+"--------------------\r\n\r\n");// armazena o texto no objeto x, que aponta para o arquivo
      //x.close();

   }
   // em caso de erro apreenta mensagem abaixo
   catch(Exception e){

   }

    for(int y=0;y<prop.length;y++){

        stm = null;
        result = null;
        consulta = "SELECT ACOD,ANOM,ACOL,CASE WHEN AESP = '1' THEN 'Canino' ELSE 'Felino' END AS ESPECIE," +
                   "CASE WHEN ASEX = '1' THEN 'Femea' ELSE 'Macho'END AS SEXO," +
                   "CASE WHEN AFIN = '1' THEN 'Companhia' ELSE 'Outros' END AS FINALIDADE  FROM CPARC.CAD_ANIM WHERE APRO = " + prop[y];

        try  {
        stm = (Statement) conexao.getConnection(true).createStatement();
        ResultSet rs = stm.executeQuery(consulta);

        while(rs.next()){

             try{
                        // o true significa q o arquivo será constante


                    x.write("Código: "+rs.getString("ACOD")+ "\t\tNome: "+rs.getString("ANOM")+ "\t\tCor: "+rs.getString("ACOL")+"\r\n"); // armazena o texto no objeto x, que aponta para o arquivo
                    x.write("Espécie: "+rs.getString("ESPECIE")+ "\t\tSexo: "+rs.getString("SEXO")+ "\t\tFinalidade: "+rs.getString("FINALIDADE")+"\r\n\r\n");

                    // cria o arquivo

            }
   // em caso de erro apreenta mensagem abaixo
            catch(Exception e){

            }
        }

        }
        catch(Exception e){
            String status = e.getMessage();
            System.out.println(status);
        }
    }
        try {
            x.close();
        } catch (IOException ex) {
           // Logger.getLogger(relatorioAnimaisporRua.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    


prop = null;
System.gc();








    }//GEN-LAST:event_btGerarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btGerar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField tfRua;
    // End of variables declaration//GEN-END:variables

}
